package ttestClosed;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class TestReceive {

    public static void main(String[] args) {

        int port = 5555; // Port to listen to
        String mcastGroup = "225.4.5.6"; // Multicast address
        // Create a socket and bind it to the port
        MulticastSocket socket;
        try {
            socket = new MulticastSocket(port);
            // join the multicast group
            socket.joinGroup(InetAddress.getByName(mcastGroup));
            // Create a DatagramPacket and a receive data
            byte buffer[] = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            socket.receive(packet);
            // Print the received data
            System.out.println("Received data from: "
                    + packet.getAddress().toString() + ":" + packet.getPort()
                    + " with length: " + packet.getLength());
            System.out.println(new String(packet.getData()));
            System.out.println();
            // Leave the multicast group and close the socket
            socket.leaveGroup(InetAddress.getByName(mcastGroup));
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
